package com.Graphics.OGL.Sphere;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.Graphics.OGL.Sphere.SphereRenderer.SphereRenderer;

public class EarthDemoActivity extends Activity
{
	private SphereSurfaceView m_glSurfaceView = null;
	LinearLayout ll;
	SphereRenderer render= null;
	Menu theMenu = null;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		m_glSurfaceView = new SphereSurfaceView(this);
		render = new SphereRenderer(this);
		m_glSurfaceView.setRenderer(render);
		m_glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); //how to use a timer?
		ll = (LinearLayout)this.findViewById(R.id.main_layout);
		ll.addView(m_glSurfaceView);
		//
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		theMenu = menu;
		new MenuInflater(getApplication()).inflate(R.menu.menu,menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		return (applyMenuChoice(item) || super.onMenuItemSelected(featureId, item));
	}

	private boolean applyMenuChoice(MenuItem item)
	{
		switch(item.getItemId())
		{
		case R.id.CHANGE_LIGHT:
			item.setChecked(!item.isChecked());
			changeLightState();
			return true;
		case R.id.ABOUT:
			Toast.makeText(this, R.string.about, Toast.LENGTH_SHORT).show();
			return true;
		}
		return false;
	}
	
	private void changeLightState()
	{
		render.lightOn = !render.lightOn;
	}


	@Override
	protected void onPause()
	{
		// TODO Auto-generated method stub
		super.onPause();
		m_glSurfaceView.onPause();
	}
	@Override
	protected void onResume()
	{
		// TODO Auto-generated method stub
		super.onResume();
		m_glSurfaceView.onResume();
	}	
}